﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace KnowledgeAssistant
{
    class TeacherUtil
    {
        public static Teacher GetTeacher(int teacher_id)
        {
            Teacher teacher = null;
            DataTable table = DatabaseConnect.execSP("GetTeacher", new List<string>() { teacher_id.ToString() }).Tables[0];
            teacher = new Teacher(
                   (int)table.Rows[0]["teacher_id"],
                   table.Rows[0]["lname"].ToString(),
                   table.Rows[0]["mname"].ToString(),
                   table.Rows[0]["fname"].ToString()
                   );
            return teacher;
        }

        public static List<Teacher> GetListTeachers()
        {
            List<Teacher> teachers = new List<Teacher>();
            DataTable table = DatabaseConnect.execSP("GetTeacher", new List<string>() { 0.ToString() }).Tables[0];

            foreach (DataRow row in table.Rows)
            {
                Teacher teacher = null;

                teacher = new Teacher(
                       (int)row["teacher_id"],
                       row["lname"].ToString(),
                       row["mname"].ToString(),
                       row["fname"].ToString()
                       );
                teachers.Add(teacher);
            }
            return teachers;
        }

        public static List<Teacher> GetListTeachersWithuserData()
        {
            List<Teacher> teachers = new List<Teacher>();
            DataTable table = DatabaseConnect.execSP("GetTeachersWithUserData",
                new List<string>() { 0.ToString() }).Tables[0];

            foreach (DataRow row in table.Rows)
            {
                Teacher teacher = null;

                teacher = new Teacher(
                       (int)row["teacher_id"],
                       row["lname"].ToString(),
                       row["mname"].ToString(),
                       row["fname"].ToString()
                       );
                teacher.Username = row["user_name"].ToString();
                teacher.Password = row["user_pass"].ToString();
                teachers.Add(teacher);
            }
            return teachers;
        }

        public static List<Teacher> GetListTeachersWithuserDataForCheckExist()
        {
            List<Teacher> teachers = new List<Teacher>();
            DataTable table = DatabaseConnect.execSP("GetTeachersWithUserData",
                new List<string>() { 0.ToString() }).Tables[0];

            foreach (DataRow row in table.Rows)
            {
                Teacher teacher = null;

                teacher = new Teacher(
                       0,
                       row["lname"].ToString(),
                       row["mname"].ToString(),
                       row["fname"].ToString()
                       );
                teachers.Add(teacher);
            }
            return teachers;
        }

        public static void SaveTeacher(Teacher teacher)
        {
            List<string> parameters = new List<string>();
            parameters.Add(teacher.LastName);
            parameters.Add(teacher.FirstName);
            parameters.Add(teacher.MiddleName);

            if (teacher.TeacherID == 0)
            {
                DatabaseConnect.execSPNonQuery("AddTeacher", parameters);
            }
            else
            {
                parameters.Add(teacher.TeacherID.ToString());
                DatabaseConnect.execSPNonQuery("UpdateTeacher", parameters);
            }
        }

        public static void DeleteTeacher(Teacher teacher)
        {
            List<string> parameters = new List<string>();
            parameters.Add(teacher.TeacherID.ToString());
            DatabaseConnect.execSPNonQuery("DeleteTeacher", parameters);
        }

        public static List<Teacher> GetListTeachersForAddUser()
        {
            List<Teacher> teachers = new List<Teacher>();
            Teacher teacher = null;
            DataTable table = DatabaseConnect.execSP("GetTeachersListToAddUser").Tables[0];

            foreach (DataRow r in table.Rows)
            {
                teacher = new Teacher(
                       int.Parse(r["teacher_id"].ToString()),
                       r["lname"].ToString(),
                       r["mname"].ToString(),
                       r["fname"].ToString());
                teachers.Add(teacher);
            }
            return teachers;
        }
    }
}
